package yanc.app

import grails.converters.JSON
import grails.transaction.Transactional
import groovy.json.JsonSlurper
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import okhttp3.*
import org.apache.commons.codec.binary.Base64
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpPost
import org.apache.http.conn.ssl.SSLConnectionSocketFactory
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClients
import org.apache.http.ssl.SSLContexts
import org.apache.http.util.EntityUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value

import javax.net.ssl.SSLContext
import java.security.KeyStore
import java.text.SimpleDateFormat

@Transactional
class PayOrderService {




    /**
     * XML格式字符串转换为Map
     *
     * @param strXML XML字符串
     * @return XML数据转换后的Map
     * @throws Exception
     */
    def Map<String, String> xmlToMap(String strXML) throws Exception {
        try {
            Map<String, String> data = new HashMap<String, String>();
            def str = new XmlParser().parseText(strXML)
            str.each{ it ->
                def a =it.name().toString().split("_")
                if (a.size() > 2 && a[1] == "status") {
                        data.put("refund_status", it.text())
                    } else {
                        data.put(it.name().toString(), it.text());
                    }
            }
            return data
        } catch (Exception ex) {
            throw ex;
        }
    }

    def String sign(Map params) {
        StringBuffer buffer = new StringBuffer()
        for (String key : params.keySet()) {
            buffer.append(key + "=" + params.get(key) + "&")
        }
        buffer.append("key=" + KEY)
        return getMD5(buffer.toString())
    }

    def String getXml(Map params) {
        StringBuffer buffer = new StringBuffer()
        buffer.append("<xml>")
        for (String key : params.keySet()) {
            buffer.append("<" + key + ">" + params.get(key) + "</" + key + ">")
        }
        buffer.append("</xml>")
        return buffer.toString()
    }

    def String getRandom() {
        String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        String res = ""
        for (int i = 0; i < 16; i++) {
            Random rd = new Random()
            res += chars.charAt(rd.nextInt(chars.length() - 1))
        }
        return res
    }

    /**
     * MD5加密运算。
     *
     * @param str 待运算的字符串。
     * @return 加密内容。
     */

    protected static String getMD5(String str) {
        return str.encodeAsMD5().toString().toUpperCase()
    }

    def Integer getInt(String a) {
        def b = Double.parseDouble(a) * 100
        def c = (int) b
        return c
    }

    def String getSign(String orderNo, String time) {
        StringBuffer buffer = new StringBuffer()
        buffer.append(orderNo)
        buffer.append(time)
        buffer.toString();
        return buffer
    }
}